from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from .models import User
from rest_framework import viewsets
from .serializers import ContentTypeSerializer, UserSerializer, GroupSerializer, PermissionSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    允许用户查看或编辑的API路径
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
    """
    允许组查看或编辑的API路径。
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer


class PermissionViewSet(viewsets.ReadOnlyModelViewSet):
    """
    只允许组查看的API路径。
    """
    # 忽略django自带model自己添加的权限
    queryset = Permission.objects.filter(pk__gt=20)
    serializer_class = PermissionSerializer


class ContentTypeViewSet(viewsets.ModelViewSet):
    """
    允许组查看或编辑的API路径。
    """
    queryset = ContentType.objects.all()
    serializer_class = ContentTypeSerializer
